package com.zingrow.web.report.response;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.zingrow.web.report.base.TableField;

/**
 * 这个类返回每个模块详细的展示信息。仅作为模块的展示数据信息返回，与模块配置的任何数据无关
 * 
 * @author sywd
 *
 */
public class ModuleData implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 6056533212787966159L;
    /**
     * 模块ID
     */
    private Integer id;
    /**
     * 模块的名称
     */
    private String name;
    /**
     * 模块显示的标题(默认为名称，也可不一样，显示时按此字段为准)
     */
    private String title;
    /**
     * 模块内说明文字(可能有格式化需要)
     */
    private String text;
    /**
     * 展示类型ID(文字-列表-图形（分类）等，具体参照展示类型id值)
     */
    private Integer typeId;
    /**
     * 额外说明(可用于扩展)
     */
    private String extra;
    /**
     * 预留横坐标说明（暂时用不到）
     */
    private String pointX;
    /**
     * 预留纵坐标说明（暂时用不到）
     */
    private String pointY;

    private Boolean show = new Boolean(true);
    /**
     * 模块内的数据（按照标题区分）
     */
    private List<Item> dataList = new ArrayList<>();
    /**
     * 标题的字段信息
     */
    @JsonIgnore
    private List<TableField> tableFields = new ArrayList<>();
    /**
     * 是否按照列表直接填充,true则是将数据填入表格的每一行,false则是每一条数据都是一列
     */
    private boolean pullByList = true;

    /**
     * 收藏字段
     */
    private Integer isCollection;

    public ModuleData(Integer id, String name, String title, String text, Integer typeId, String extra, String pointX,
            String pointY) {
        this.id = id;
        this.name = name;
        this.title = title;
        this.text = text;
        this.typeId = typeId;
        this.extra = extra;
        this.pointX = pointX;
        this.pointY = pointY;
    }

    public ModuleData() {
    }

    /**
     * 添加数据列表到数据Map
     * 
     * @param name
     *            标题
     * @param list
     *            数据信息
     * @return 当前展示数据Map
     */
    public void addDataList(String key, List<? extends BaseData> datas) {
        if (key == null || StringUtils.isBlank(key.toString())) {
            return;
        }
        Item item = new Item(key, datas);
        dataList.add(item);
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getTitle() {
        if (StringUtils.isBlank(title)) {
            setTitle(getName());
        }
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public Integer getTypeId() {
        return typeId;
    }

    public void setTypeId(Integer typeId) {
        this.typeId = typeId;
    }

    public String getExtra() {
        return extra;
    }

    public void setExtra(String extra) {
        this.extra = extra;
    }

    public String getPointX() {
        return pointX;
    }

    public void setPointX(String pointX) {
        this.pointX = pointX;
    }

    public String getPointY() {
        return pointY;
    }

    public void setPointY(String pointY) {
        this.pointY = pointY;
    }

    public List<Item> getDataList() {
        if (dataList == null || (dataList.size() == 1 && (null == dataList.get(0)
                || (dataList.get(0).getDatas() == null || dataList.get(0).getDatas().get(0) == null)))) {
            clearDataList();
        }
        return dataList;
    }

    public void setDataList(List<Item> dataList) {
        this.dataList = dataList;
    }

    public Integer getIsCollection() {
        return isCollection;
    }

    public void setIsCollection(Integer isCollection) {
        this.isCollection = isCollection;
    }

    public List<TableField> getTableFields() {
        return tableFields;
    }

    public void setTableFields(List<TableField> tableFields) {
        this.tableFields = tableFields;
    }

    public ModuleData addTableField(TableField tableField) {
        getTableFields().add(tableField);
        return this;
    }


    public boolean isPullByList() {
        return pullByList;
    }

    public void setPullByList(boolean pullByList) {
        this.pullByList = pullByList;
    }

    /**
     * 清除数据列表
     */
    public void clearDataList() {
        setDataList(new ArrayList<Item>());
    }

    public Boolean getShow() {
        try {
            if (StringUtils.isBlank(getText()) && (getDataList() == null || getDataList().size() == 0)) {
                setShow(false);
            }
        } catch (Exception e) {
            clearDataList();
            setShow(false);
        }
        return show;
    }

    public void setShow(Boolean show) {
        this.show = show;
    }

    public class Item {
        public String title;
        public List<? extends BaseData> datas;

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public List<? extends BaseData> getDatas() {
            return datas;
        }

        public void setDatas(List<? extends BaseData> datas) {
            this.datas = datas;
        }

        public Item() {
        }

        public Item(String title, List<? extends BaseData> datas) {
            this.title = title;
            this.datas = datas;
        }
    }

}
